<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<%@include file="/common/taglibs.jsp"%>
<%@include file="/common/meta.jsp"%>
<title>资源管理</title>
</head>
<body>
	<%@include file="/common/header.jsp"%>
	
	<div id="resourceTree" class="ztree" style="float: left;width: 11%"></div>
	
	<div id="resourceGrid"></div>
	
	<%@include file="/common/footer.jsp"%>
	
	<script type="text/javascript">
		var setting = {
			async: {
				enable: true,
				url: "<c:url value='/pf/resource/ztree'/>"
			},
			callback: {
				onAsyncSuccess: zTreeOnAsyncSuccess,
				onAsyncError: zTreeOnAsyncError,
				onClick: zTreeOnClick
			},
			data: {
				simpleData: {
					enable: true,
					rootPId: "-1"
				}
			},
			view: {
				selectedMulti: false
			}
		};
		
		//异步正常加载结束回调函数
		function zTreeOnAsyncSuccess(event, treeId, treeNode, msg) {
			var treeObj = $.fn.zTree.getZTreeObj(treeId);
			var nodes = treeObj.getNodes();
			if (nodes.length > 0) {
				treeObj.expandNode(nodes[0]);
			}
		}
		
		//异步异常加载回调函数
		function zTreeOnAsyncError(event, treeId, treeNode, XMLHttpRequest, textStatus, errorThrown) {
			console.log(treeId, treeNode, textStatus);
		}
		
		//单击事件
		function zTreeOnClick(event, treeId, treeNode) {
			var treeObj = $.fn.zTree.getZTreeObj(treeId);
			var nodes = treeObj.getSelectedNodes();
			$("#resourceGrid").flexOptions({
				params : [ 
				           {name: "id", value: nodes[0].id}
						]
			}).flexReload();
		}
		
		$(function() {
			$.fn.zTree.init($("#resourceTree"), setting, null);
			
			$("#resourceGrid").flexigrid({
				title: "资源信息列表",
				url: "<c:url value='/pf/resource/dataGrid'/>",
				colModel: [
					{display: "资源名称", name: "resourceName"},
					{display: "上级资源名称", name: "resourceParentName"},
					{display: "资源地址", name: "resourceUrl"},
					{display: "资源图标地址", name: "resourceIcon"},
					{display: "排序值", name: "resourceOrderno"},
					{display: "是否有效", name: "resourceIsvalid"}
				],
				buttons: [
					{name: "新增", bclass: "add", onpress: btns},
					{separator: true},
					{name: "修改", bclass: "modify", onpress: btns},          
					{separator: true},
					{name: "删除", bclass: "delete", onpress: btns},
					{separator: true}
				],
				usepager: true,
				height:$(window).height()-210
			});
		});
		
		//操作
		function btns(com, grid) {
			if("新增" == com) {
				var nodes = $.fn.zTree.getZTreeObj("resourceTree").getSelectedNodes();
				if(0 == nodes.length) {
					art.dialog.alert("请选择要创建的上级资源！");
					return false;
				}
				
				var pId = $.fn.zTree.getZTreeObj("resourceTree").getSelectedNodes()[0].id;
				art.dialog.open(
					"<c:url value='/pf/resource/faResource?pId=" + pId + "'/>", 
					{
						id: "addResource",
						title: "增加资源",
						lock: true,
						width: 350,
						height: 300
					}
				);
			} else if($(".trSelected", grid).length == 1) {
				var id = $(".trSelected", grid).attr("id").replace("row", "");
				if("修改" == com) {
					art.dialog.open(
						"<c:url value='/pf/resource/fmResource?resourceId=" + id + "'/>", 
						{
							id: "modifyResource",
							title: "修改资源",
							lock: true,
							width: 350,
							height: 300
						}
					);
				} else if("删除" == com) {
					$.post(
						"<c:url value='/pf/resource/dResource'/>",
						{"resourceId": id},
						function(data, status) {
							if("success" == status) {
								var treeObj = $.fn.zTree.getZTreeObj("resourceTree");
								treeObj.reAsyncChildNodes(null, "refresh");
								$("#resourceGrid").flexReload();
							} else {
								art.dialog.alert(data.msg);
							}
						}
					);
				}
			} else {
				art.dialog.alert("请选择要" + com + "的资源！");
			}
		}
		
		//刷新页面
		function closeAndRefresh() {
			var treeObj = $.fn.zTree.getZTreeObj("resourceTree");
			treeObj.reAsyncChildNodes(null, "refresh");
			$("#resourceGrid").flexReload();
			
			$.each(art.dialog.list, function(i, n) {
				art.dialog.list[i].close();
			});
		}
	</script>
</body>
</html>